package com.atguigu.controller;

import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class BloomFilterController {

    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private RBloomFilter skuRBloomFilter;

    @GetMapping("bloom")
    public String setBloom(){
        //如果数据库有变化 还需要同步数据库,直接删除布隆
        skuRBloomFilter.delete();
        //再次初始化布隆过滤器大小与容错
        skuRBloomFilter.tryInit(10000,0.001);
        //加载数据库所有id
        QueryWrapper<SkuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        List<SkuInfo> skuInfoList = skuInfoService.list(queryWrapper);
        for (SkuInfo skuInfo : skuInfoList) {
            Long skuInfoId = skuInfo.getId();
            //放入布隆过滤器
            skuRBloomFilter.add(skuInfoId);
        }
        return "success";
    }

}
